package com.wiscamp.ninechapters.problems.application.services;

import com.wiscamp.ninechapters.problems.application.assemblers.CollectionPartAssembler;
import com.wiscamp.ninechapters.problems.application.dto.commands.collections.CreateCollectionPartCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.collections.CreateCollectionSectionCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.collections.UpdateCollectionPartCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.collections.UpdateCollectionSectionCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.collections.CollectionPartVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.collections.ColllectionSectionVO;
import com.wiscamp.ninechapters.problems.domain.repositories.CollectionPartRepository;
import com.wiscamp.ninechapters.problems.domain.services.CollectionPartService;
import cube.ddd.application.BaseAppService;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.aspect.HandleServiceException;
import cube.ddd.utils.AppServiceUtils;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class CollectionPartAppService extends BaseAppService {

    private final CollectionPartRepository collectionPartRepository;
    private final CollectionPartService collectionPartService;

    public CollectionPartAppService(CollectionPartRepository collectionPartRepository, CollectionPartService collectionPartService) {
        this.collectionPartRepository = collectionPartRepository;
        this.collectionPartService = collectionPartService;
    }


    //region part
    @HandleServiceException
    public CollectionPartVO getCollectionPart(@NotNull long partId) {
        return AppServiceUtils.getById(partId, collectionPartRepository::getCollectionPart,
                CollectionPartAssembler.Instance::toCollectionPartVO);
    }

    @HandleServiceException
    public Collection<CollectionPartVO> getCollectionParts(@NotNull long collectionId) {
        return AppServiceUtils.getListById(collectionId, collectionPartRepository::getCollectionParts,
                CollectionPartAssembler.Instance::toCollectionPartVO);
    }

    @HandleServiceException
    public CollectionPartVO createCollectionPart(@NotNull CreateCollectionPartCommand command) {
        return AppServiceUtils.createOrUpdateBy(command, command.getOperatorId(), collectionPartService::createCollectionPart,
                CollectionPartAssembler.Instance::toCollectionPartVO, CollectionPartAssembler.Instance::fromCreateCommand);
    }

    @HandleServiceException
    public CollectionPartVO updateCollectionPart(@NotNull UpdateCollectionPartCommand command) {
        return AppServiceUtils.createOrUpdateBy(command, command.getOperatorId(), collectionPartService::updateCollectionPart,
                CollectionPartAssembler.Instance::toCollectionPartVO, CollectionPartAssembler.Instance::fromUpdateCommand);
    }

    @HandleServiceException
    public boolean deleteCollectionPart(@NotNull DeleteCommand command) {
        return AppServiceUtils.delete(command.getId(), command.getOperatorId(), collectionPartService::deleteCollectionPart);
    }

    // endregion

    // region section
    @HandleServiceException
    public ColllectionSectionVO getCollectionSection(@Min(0) long sectionId) {
        return AppServiceUtils.getById(sectionId, collectionPartRepository::getCollectionSection,
                CollectionPartAssembler.Instance::toCollectionSectionVO);
    }

    @HandleServiceException
    public Collection<ColllectionSectionVO> getCollectionSections(@Min(0) long partId) {
        return AppServiceUtils.getListById(partId, collectionPartRepository::getCollectionSections,
                CollectionPartAssembler.Instance::toCollectionSectionVO);
    }

    @HandleServiceException
    public ColllectionSectionVO createCollectionSection(@NotNull CreateCollectionSectionCommand command) {
        return AppServiceUtils.createOrUpdateBy(command, command.getOperatorId(), collectionPartService::createCollectionSection,
                CollectionPartAssembler.Instance::toCollectionSectionVO, CollectionPartAssembler.Instance::fromCreateCommand);
    }

    @HandleServiceException
    public ColllectionSectionVO updateCollectionSection(@NotNull UpdateCollectionSectionCommand command) {
        return AppServiceUtils.createOrUpdateBy(command, command.getOperatorId(), collectionPartService::updateCollectionSection,
                CollectionPartAssembler.Instance::toCollectionSectionVO, CollectionPartAssembler.Instance::fromUpdateCommand);
    }

    @HandleServiceException
    public boolean deleteCollectionSection(@NotNull DeleteCommand command) {
        return AppServiceUtils.delete(command.getId(), command.getOperatorId(), collectionPartService::deleteCollectionSection);
    }
    //endregion
}
